#include "Mtmodebus.h"
#include <stdio.h>
#include "main.h"
#include "systemdata.h"
#include <stdlib.h>
#include "BatMonitor.h"
#include "bat_afe.h"
#include "drv_rtc.h"
#include "drv_usart1.h"
#include "worktask.h"
#include "XgUpperMonitor.h"

static uint8_t send_data[256];

static void modbus_send_data(uint8_t *data,uint16_t len,uint8_t comNum){
	if(comNum == 1){
		usart1_send(data,len);
	}else if(comNum == 2){
		usart2_send(data,len);
	}else if(comNum == 4){
		usart4_send(data,len);
	}
}

//发送开关量
static void sendSwitchValue(uint8_t *data,uint16_t dataLen,uint8_t comNum){
	SysRunData_t  *runInfo = getSysRunData();
	uint16_t regStartAddr = data[2]&0xff;
	regStartAddr <<= 8;
	regStartAddr |= data[3]&0xff;
	uint16_t regCount = data[4]&0xff;
	regCount <<= 8;
	regCount |= data[5]&0xff;
	uint8_t byteCnt = regCount/8 + (bool)(regCount%8);//计算出发送的字节数
	memset(send_data,0,256);
	uint8_t swByte[8] = {0};
	uint8_t index = 0;
	send_data[index ++] = data[0]&0xff;
	if(regStartAddr + regCount > 64){
		send_data[index ++] = (data[1] | 0x80)&0xff;
		send_data[index ++] = 0x02;
		unsigned short crcValue = XgBmsProtocolCrc(send_data, index);
		send_data[index++] = (crcValue >> 8)& 0xff;
		send_data[index++] = crcValue&0xff;
		modbus_send_data(send_data,index,comNum);
		return;
	}else{
		send_data[index ++] = data[1]&0xff;
	}
	send_data[index ++] = byteCnt&0xff;
	uint8_t tempValue = 0;
	for(uint16_t i= 0;i <regCount;i++,regStartAddr ++){
		uint8_t errorBit = 0;
		switch(regStartAddr){
			case 0:{//保留位
				errorBit |= (0x0 << (i%8));//默认为0
			}break;
			case 1:{//单体压差保护				
				errorBit |= runInfo->volProtByte.volProt.bit.cellVolDiffProt;
				errorBit <<= (i%8);
			}break;
			case 2:{//充电过流				
				errorBit |= runInfo->curProtByte.bit.chgCUr1Prot;
				errorBit |= runInfo->curProtByte.bit.chgCur2Prot;
				errorBit |= runInfo->curProtByte.bit.chgCur3Prot;
				errorBit <<= (i%8);
			}break;
			case 3:{//放电过流				
				errorBit |= runInfo->curProtByte.bit.disCur1Prot;
				errorBit |= runInfo->curProtByte.bit.disCur2Prot;
				errorBit |= runInfo->curProtByte.bit.disCur3Prot;
				errorBit <<= (i%8);
			}break;
			case 4:{//短路保护				
				errorBit |= runInfo->curProtByte.bit.shortCurProt;
				errorBit <<= (i%8);
			}break;
			case 5:{//充电高温保护				
				errorBit |= runInfo->tempProtByte.bit.chgTempHgProt;
				errorBit <<= (i%8);
			}break;
			case 6:{//放电高温保护				
				errorBit |= runInfo->tempProtByte.bit.disTempHgProt;
				errorBit <<= (i%8);
			}break;
			case 7:{//充电低温保护				
				errorBit |= runInfo->tempProtByte.bit.chgTempLowProt;
				errorBit <<= (i%8);
			}break;
			case 8:{//放电低温保护				
				errorBit |= runInfo->tempProtByte.bit.disTempLowProt;
				errorBit <<= (i%8);
			}break;
			case 9:{//充电mos损坏				
				errorBit |= 0x0;//默认为0
				errorBit <<= (i%8);
			}break;
			case 10:{//放电mos损坏				
				errorBit |= 0x0;//默认为0	
				errorBit <<= (i%8);
			}break;
			case 11:{//内部通讯故障				
				errorBit |= runInfo->afeProtByte.bit.comFaultProt;
				errorBit <<= (i%8);
			}break;
			case 12:{//单体1过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>0)&0x01);
				errorBit <<= (i%8);
			}break;
			case 13:{//单体2过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>1)&0x01);
				errorBit <<= (i%8);
			}break;
			case 14:{//单体3过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>2)&0x01);
				errorBit <<= (i%8);
			}break;
			case 15:{//单体4过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>3)&0x01);
				errorBit <<= (i%8);
			}break;
			case 16:{//单体5过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>4)&0x01);
				errorBit <<= (i%8);
			}break;
			case 17:{//单体6过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>5)&0x01);
				errorBit <<= (i%8);
			}break;
			case 18:{//单体7过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>6)&0x01);
				errorBit <<= (i%8);
			}break;
			case 19:{//单体8过压保护			
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>7)&0x01);
				errorBit <<= (i%8);
			}break;
			case 20:{//单体9过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>8)&0x01);
				errorBit <<= (i%8);
			}break;
			case 21:{//单体10过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>9)&0x01);
				errorBit <<= (i%8);
			}break;
			case 22:{//单体11过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>10)&0x01);
				errorBit <<= (i%8);
			}break;
			case 23:{//单体12过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>11)&0x01);
				errorBit <<= (i%8);
			}break;
			case 24:{//单体13过压保护		
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>12)&0x01);
				errorBit <<= (i%8);
			}break;
			case 25:{//单体14过压保护			
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>13)&0x01);
				errorBit <<= (i%8);
			}break;
			case 26:{//单体15过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>14)&0x01);
				errorBit <<= (i%8);
			}break;
			case 27:{//单体16过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>15)&0x01);
				errorBit <<= (i%8);
			}break;
			case 28:{//单体17过压保护			
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>16)&0x01);
				errorBit <<= (i%8);
			}break;
			case 29:{//单体18过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>17)&0x01);
				errorBit <<= (i%8);
			}break;
			case 30:{//单体19过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>18)&0x01);
				errorBit <<= (i%8);
			}break;
			case 31:{//单体20过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>19)&0x01);
				errorBit <<= (i%8);
			}break;
			case 32:{//单体1欠压保护				
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>0)&0x01);
				errorBit <<= (i%8);
			}break;
			case 33:{//单体2欠压保护				
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>1)&0x01);
				errorBit <<= (i%8);
			}break;
			case 34:{//单体3欠压保护				
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>2)&0x01);
				errorBit <<= (i%8);
			}break;
			case 35:{//单体4欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>3)&0x01);
				errorBit <<= (i%8);
			}break;
			case 36:{//单体5欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>4)&0x01);
				errorBit <<= (i%8);
			}break;
			case 37:{//单体6欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>5)&0x01);
				errorBit <<= (i%8);
			}break;
			case 38:{//单体7欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>6)&0x01);
				errorBit <<= (i%8);
			}break;
			case 39:{//单体8欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>7)&0x01);
				errorBit <<= (i%8);
			}break;
			case 40:{//单体9欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>8)&0x01);
				errorBit <<= (i%8);
			}break;
			case 41:{//单体10欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>9)&0x01);
				errorBit <<= (i%8);
			}break;
			case 42:{//单体11欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>10)&0x01);
				errorBit <<= (i%8);
			}break;
			case 43:{//单体12欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>11)&0x01);
				errorBit <<= (i%8);
			}break;
			case 44:{//单体13欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>12)&0x01);
				errorBit <<= (i%8);
			}break;
			case 45:{//单体14欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>13)&0x01);
				errorBit <<= (i%8);
			}break;
			case 46:{//单体15欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>14)&0x01);
				errorBit <<= (i%8);
			}break;
			case 47:{//单体16欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>15)&0x01);
				errorBit <<= (i%8);
			}break;
			case 48:{//单体17欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>16)&0x01);
				errorBit <<= (i%8);
			}break;
			case 49:{//单体18欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>17)&0x01);
				errorBit <<= (i%8);
			}break;
			case 50:{//单体19欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>18)&0x01);
				errorBit <<= (i%8);
			}break;
			case 51:{//单体20欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>19)&0x01);
				errorBit <<= (i%8);
			}break;
			case 52:{//单体21过压保护
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>20)&0x01);
				errorBit <<= (i%8);
			}break;
			case 53:{//单体22过压保护
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>21)&0x01);
				errorBit <<= (i%8);
			}break;
			case 54:{//单体23过压保护
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>22)&0x01);
				errorBit <<= (i%8);
			}break;
			case 55:{//单体24过压保护				
				errorBit |= ((runInfo->volProtByte.cellHgVolProt>>23)&0x01);
				errorBit <<= (i%8);
			}break;
			case 56:{//单体21欠压保护				
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>20)&0x01);
				errorBit <<= (i%8);
			}break;
			case 57:{//单体22欠压保护
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>21)&0x01);
				errorBit <<= (i%8);
			}break;
			case 58:{//单体23欠压保护				
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>22)&0x01);
				errorBit <<= (i%8);
			}break;
			case 59:{//单体24欠压保护				
				errorBit |= ((runInfo->volProtByte.cellLowVolProt>>23)&0x01);
				errorBit <<= (i%8);
			}break;
			case 60:{//
				errorBit <<= (i%8);
			}break;
			case 61:{//
				errorBit <<= (i%8);
			}break;
			case 62:{//
				errorBit <<= (i%8);
			}break;
			case 63:{//
				errorBit <<= (i%8);
			}break;
		}
		tempValue |= errorBit;
		if(i%8 == 7 || i == (regCount - 1)){
			send_data[index++] = (tempValue&0xff);
			tempValue = 0;
		}
	}
	unsigned short crcValue = XgBmsProtocolCrc(send_data, index);
	send_data[index++] = (crcValue >> 8)& 0xff;
	send_data[index++] = crcValue&0xff;
	modbus_send_data(send_data,index,comNum);
}

//发送模拟量
static void sendAnalogValue(uint8_t *data,uint16_t dataLen,uint8_t comNum){
	SysRunData_t  *runInfo = getSysRunData();
	SysConfData_t *confInfo = getSysConfData();
	uint16_t regStartAddr = data[2]&0xff;
	regStartAddr <<= 8;
	regStartAddr |= data[3]&0xff;
	uint16_t regCount = data[4]&0xff;
	regCount <<= 8;
	regCount |= data[5]&0xff;
	memset(send_data,0,256);
	uint8_t index = 0;
	send_data[index ++] = data[0]&0xff;

	uint16_t regAddr = regStartAddr + regCount - 1;
	if((regAddr > 45 && regAddr < 100) ||  (regAddr > 115 && regAddr < 1000) ||  (regAddr > 1015 && regAddr < 1100) || (regAddr > 1115)){
		send_data[index ++] = (data[1] | 0x80)&0xff;
		send_data[index ++] = 0x02;//地址无效或超范围
		unsigned short crcValue = XgBmsProtocolCrc(send_data, index);
		send_data[index++] = (crcValue >> 8)& 0xff;
		send_data[index++] = crcValue&0xff;
		modbus_send_data(send_data,index,comNum);

		return;
	}
	send_data[index ++] = data[1]&0xff;
	send_data[index ++] = (regCount*2)&0xff;
	for(uint16_t i = 0;i<regCount;i++,regStartAddr++){
		switch(regStartAddr){
			case 0:{//电池总压:0.01V
				uint16_t temp_packVol = 0;
				temp_packVol = runInfo->packVol*10;
				send_data[index ++] = (temp_packVol >> 8) & 0xff;
				send_data[index ++] = (temp_packVol >> 0) & 0xff;
			}break;
			case 1:{//电芯数量
				send_data[index ++] = (confInfo->fctData.batNum >> 8) & 0xff;
				send_data[index ++] = (confInfo->fctData.batNum >> 0) & 0xff;
			}break;
			case 2:{//soc:1%
				send_data[index ++] = ((runInfo->batSoc) >> 8) & 0xff;
				send_data[index ++] = ((runInfo->batSoc) >> 0) & 0xff;
			}break;
			case 3:{//剩余电量:0.01Ah
				uint16_t remCap = runInfo->rmCap_mAms/36000000;
				send_data[index++] = (remCap>>8)&0xff;
				send_data[index++] = remCap&0xff;
			}break;
			case 4:{//明唐V1.5协议:放电电流:0.01A;铁塔V1.1协议:SOH
				uint16_t regValue = 0;
				regValue = runInfo->batSoh;
				//regValue = runInfo->batCur/10;
				send_data[index++] = (regValue>>8)&0xff;
				send_data[index++] = (regValue>>0)&0xff;
			}break;
			case 5:{//充电电流:0.01A
				send_data[index++] = ((runInfo->batCur/10)>>8)&0xff;
				send_data[index++] = ((runInfo->batCur/10)>>0)&0xff;
			}break;
			case 6:{//环境温度:1℃
				int16_t temp = runInfo->envTemp;
				send_data[index++] = (temp>>8)&0xff;
				send_data[index++] = temp&0xff;
			}break;
			case 7:{//电池温度1:1℃
				int16_t temp = runInfo->tempOne;
				send_data[index++] = (temp>>8)&0xff;
				send_data[index++] = temp&0xff;
			}break;
			case 8:{//板卡温度:1℃
				int16_t temp = runInfo->mosTemp;
				send_data[index++] = (temp>>8)&0xff;
				send_data[index++] = temp&0xff;
			}break;
			case 9:{//单体1电压:1mV
				send_data[index++] = (runInfo->batVol[0]>>8)&0xff;
				send_data[index++] = runInfo->batVol[0]&0xff;
			}break;
			case 10:{//单体2电压:1mV
				send_data[index++] = (runInfo->batVol[1]>>8)&0xff;
				send_data[index++] = runInfo->batVol[1]&0xff;
			}break;
			case 11:{//单体3电压:1mV
				send_data[index++] = (runInfo->batVol[2]>>8)&0xff;
				send_data[index++] = runInfo->batVol[2]&0xff;
			}break;
			case 12:{//单体4电压:1mV
				send_data[index++] = (runInfo->batVol[3]>>8)&0xff;
				send_data[index++] = runInfo->batVol[3]&0xff;
			}break;
			case 13:{//单体5电压:1mV
				send_data[index++] = (runInfo->batVol[4]>>8)&0xff;
				send_data[index++] = runInfo->batVol[4]&0xff;
			}break;
			case 14:{//单体6电压:1mV
				send_data[index++] = (runInfo->batVol[5]>>8)&0xff;
				send_data[index++] = runInfo->batVol[5]&0xff;
			}break;
			case 15:{//单体7电压:1mV
				send_data[index++] = (runInfo->batVol[6]>>8)&0xff;
				send_data[index++] = runInfo->batVol[6]&0xff;
			}break;
			case 16:{//单体8电压:1mV
				send_data[index++] = (runInfo->batVol[7]>>8)&0xff;
				send_data[index++] = runInfo->batVol[7]&0xff;
			}break;
			case 17:{//单体9电压:1mV
				send_data[index++] = (runInfo->batVol[8]>>8)&0xff;
				send_data[index++] = runInfo->batVol[8]&0xff;
			}break;
			case 18:{//单体10电压:1mV
				send_data[index++] = (runInfo->batVol[9]>>8)&0xff;
				send_data[index++] = runInfo->batVol[9]&0xff;
			}break;
			case 19:{//单体11电压:1mV
				send_data[index++] = (runInfo->batVol[10]>>8)&0xff;
				send_data[index++] = runInfo->batVol[10]&0xff;
			}break;
			case 20:{//单体12电压:1mV
				send_data[index++] = (runInfo->batVol[11]>>8)&0xff;
				send_data[index++] = runInfo->batVol[11]&0xff;
			}break;
			case 21:{//单体13电压:1mV
				send_data[index++] = (runInfo->batVol[12]>>8)&0xff;
				send_data[index++] = runInfo->batVol[12]&0xff;
			}break;
			case 22:{//单体14电压:1mV
				send_data[index++] = (runInfo->batVol[13]>>8)&0xff;
				send_data[index++] = runInfo->batVol[13]&0xff;
			}break;
			case 23:{//单体15电压:1mV
				send_data[index++] = (runInfo->batVol[14]>>8)&0xff;
				send_data[index++] = runInfo->batVol[14]&0xff;
			}break;
			case 24:{//单体16电压:1mV
				send_data[index++] = (runInfo->batVol[15]>>8)&0xff;
				send_data[index++] = runInfo->batVol[15]&0xff;
			}break;
			case 25:{//单体17电压:1mV
				send_data[index++] = (runInfo->batVol[16]>>8)&0xff;
				send_data[index++] = runInfo->batVol[16]&0xff;
			}break;
			case 26:{//单体18电压:1mV
				send_data[index++] = (runInfo->batVol[17]>>8)&0xff;
				send_data[index++] = runInfo->batVol[17]&0xff;
			}break;
			case 27:{//单体19电压:1mV
				send_data[index++] = (runInfo->batVol[18]>>8)&0xff;
				send_data[index++] = runInfo->batVol[18]&0xff;
			}break;
			case 28:{//单体20电压:1mV
				send_data[index++] = (runInfo->batVol[19]>>8)&0xff;
				send_data[index++] = runInfo->batVol[19]&0xff;
			}break;
			case 29:{//单体21电压:1mV
				send_data[index++] = (runInfo->batVol[20]>>8)&0xff;
				send_data[index++] = runInfo->batVol[20]&0xff;
			}break;
			case 30:{//单体22电压:1mV
				send_data[index++] = (runInfo->batVol[21]>>8)&0xff;
				send_data[index++] = runInfo->batVol[21]&0xff;
			}break;
			case 31:{//单体23电压:1mV
				send_data[index++] = (runInfo->batVol[22]>>8)&0xff;
				send_data[index++] = runInfo->batVol[22]&0xff;
			}break;
			case 32:{//单体24电压:1mV
				send_data[index++] = (runInfo->batVol[23]>>8)&0xff;
				send_data[index++] = runInfo->batVol[23]&0xff;
			}break;
			case 41:{//充电电流请求值:0.1A
				send_data[index++] = (runInfo->chgReqCur>>8)&0xff;
				send_data[index++] = runInfo->chgReqCur&0xff;
			}break;
			case 42:{//充电电压请求值:0.1V
				send_data[index++] = (runInfo->chgReqVol>>8)&0xff;
				send_data[index++] = runInfo->chgReqVol&0xff;
			}break;
			case 43:{//BMS充电标志,柜厂建议写0x0000（使用充电机默认充电策略）。0x0055：使用bms请求的电压和电流进行充电，0x00AA：充电器停止输出。
				send_data[index++] = (0>>8)&0xff;
				send_data[index++] = 0&0xff;
			}break;
			case 44:{//加热功能开关状态。0x0000：禁止加热，0x0001：允许加热
				send_data[index++] = (0>>8)&0xff;
				send_data[index++] = 0&0xff;
			}break;
			case 45:{//电池内部加热状态。默认关闭
				send_data[index++] = (0>>8)&0xff;
				send_data[index++] = 0&0xff;
			}break;
			case 100:{//充电开关
				send_data[index++] = (runInfo->sysSwState.bits.SW_CHG>>8)&0xff;
				send_data[index++] = runInfo->sysSwState.bits.SW_CHG&0xff;
			}break;
			case 101:{//放电开关
				send_data[index++] = (runInfo->sysSwState.bits.SW_DSG>>8)&0xff;
				send_data[index++] = runInfo->sysSwState.bits.SW_DSG&0xff;
			}break;
			case 102:{//标称电压:0.01V
				uint16_t normalPackVol = 0;
				if(confInfo->fctData.batType == sanyuanBat){//三元电池
					normalPackVol = 370*confInfo->fctData.batNum;
				}else if(confInfo->fctData.batType == lstlBat){//铁锂电池
					normalPackVol = 320*confInfo->fctData.batNum;
				}else if(confInfo->fctData.batType == nslBat){
					normalPackVol = 300*confInfo->fctData.batNum;
				}else{
					normalPackVol = 370*confInfo->fctData.batNum;
				}
				send_data[index++] = (normalPackVol>>8)&0xff;
				send_data[index++] = normalPackVol&0xff;
			}break;
			case 103:{//电池类型:0、三元,1、铁锂
				uint16_t batType = 0;
				if(confInfo->fctData.batType == sanyuanBat){//三元电池
					batType = 0;
				}else if(confInfo->fctData.batType == lstlBat){//铁锂电池
					batType = 1;
				}else if(confInfo->fctData.batType == nslBat){
					batType = 2;
				}else{
					batType = 0;
				}
				send_data[index++] = (batType>>8)&0xff;
				send_data[index++] = batType&0xff;
			}break;
			case 104:{//循环次数
				send_data[index++] = (confInfo->fctData.cycleTimes.value>>8)&0xff;
				send_data[index++] = confInfo->fctData.cycleTimes.value&0xff;
			}break;
			case 105:{//SOH:1%
				send_data[index ++] = ((runInfo->batSoh/10) >> 8) & 0xff;
				send_data[index ++] = ((runInfo->batSoh/10) >> 0) & 0xff;
			}break;
			case 106:{//故障码H
				send_data[index ++] = (0 >> 8) & 0xff;
				send_data[index ++] = (0 >> 0) & 0xff;
			}break;
			case 107:{//故障码L
				send_data[index ++] = (0 >> 8) & 0xff;
				send_data[index ++] = (0 >> 0) & 0xff;
			}break;
			case 108:{//扩展温度1:1℃,当前传电池温度2
				int16_t temp = runInfo->tempTwo;
				send_data[index++] = (temp>>8)&0xff;
				send_data[index++] = temp&0xff;
			}break;
			case 109:{//扩展温度2:1℃
				send_data[index ++] = (0 >> 8) & 0xff;
				send_data[index ++] = (0 >> 0) & 0xff;
			}break;
			case 110:{//均衡状态:整组电池是否在均衡,0、未均衡,1、正在均衡
				uint8_t balState = (bool)runInfo->balState;
				send_data[index ++] = (balState >> 8) & 0xff;
				send_data[index ++] = (balState >> 0) & 0xff;
			}break;
			case 111:{//单体最大压差:1mV
				send_data[index++] = (runInfo->maxDiffVol>>8)&0xff;
				send_data[index++] = runInfo->maxDiffVol&0xff;
			}break;
			case 112:{//低温加热开关状态
				send_data[index++] = (runInfo->sysSwState.bits.SW_HT>>8)&0xff;
				send_data[index++] = runInfo->sysSwState.bits.SW_HT&0xff;
			}break;
			case 113:{//电池标称容量:1Ah，返回5的倍数
				uint16_t normalCap = confInfo->fctData.normalCap.value/50;
				send_data[index ++] = (normalCap >> 8) & 0xff;
				send_data[index ++] = (normalCap >> 0) & 0xff;
			}break;
			case 114:{//充电MOS电流:1A
				send_data[index++] = ((runInfo->batCur/1000)>>8)&0xff;
				send_data[index++] = ((runInfo->batCur/1000)>>0)&0xff;
			}break;
			case 115:{//放电MOS电流:1A
				send_data[index++] = ((runInfo->batCur/1000)>>8)&0xff;
				send_data[index++] = ((runInfo->batCur/1000)>>0)&0xff;
			}break;
			case 1000:{//设备ID[1]
				send_data[index++] = (confInfo->fctData.BtNum[0])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[1])&0xff;
			}break;
			case 1001:{//设备ID[2]
				send_data[index++] = (confInfo->fctData.BtNum[2])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[3])&0xff;
			}break;
			case 1002:{//设备ID[3]
				send_data[index++] = (confInfo->fctData.BtNum[4])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[5])&0xff;
			}break;
			case 1003:{//设备ID[4]
				send_data[index++] = (confInfo->fctData.BtNum[6])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[7])&0xff;
			}break;
			case 1004:{//设备ID[5]
				send_data[index++] = (confInfo->fctData.BtNum[8])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[9])&0xff;
			}break;
			case 1005:{//设备ID[6]
				send_data[index++] = (confInfo->fctData.BtNum[10])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[11])&0xff;
			}break;
			case 1006:{//设备ID[7]
				send_data[index++] = (confInfo->fctData.BtNum[12])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[13])&0xff;
			}break;
			case 1007:{//设备ID[8]
				send_data[index++] = (confInfo->fctData.BtNum[14])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[15])&0xff;
			}break;
			case 1008:{//设备ID[9]
				send_data[index++] = (confInfo->fctData.BtNum[16])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[17])&0xff;
			}break;
			case 1009:{//设备ID[10]
				send_data[index++] = (confInfo->fctData.BtNum[18])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[19])&0xff;
			}break;
			case 1010:{//设备ID[11]
				send_data[index++] = (confInfo->fctData.BtNum[20])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[21])&0xff;
			}break;
			case 1011:{//设备ID[12]
				send_data[index++] = (confInfo->fctData.BtNum[22])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[23])&0xff;
			}break;
			case 1012:{//设备ID[13]
				send_data[index++] = (0)&0xff;
				send_data[index++] = (0)&0xff;
			}break;
			case 1013:{//设备ID[14]
				send_data[index++] = (0)&0xff;
				send_data[index++] = (0)&0xff;
			}break;
			case 1014:{//保留
				send_data[index++] = (0)&0xff;
				send_data[index++] = (0)&0xff;
			}break;
			case 1015:{//保留
				send_data[index++] = (0)&0xff;
				send_data[index++] = (0)&0xff;
			}break;
			case 1100:{//设备副ID[1]
				send_data[index++] = (confInfo->fctData.BtNum[0])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[1])&0xff;
			}break;
			case 1101:{//设备副ID[2]
				send_data[index++] = (confInfo->fctData.BtNum[2])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[3])&0xff;
			}break;
			case 1102:{//设备副ID[3]
				send_data[index++] = (confInfo->fctData.BtNum[4])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[5])&0xff;
			}break;
			case 1103:{//设备副ID[4]
				send_data[index++] = (confInfo->fctData.BtNum[6])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[7])&0xff;
			}break;
			case 1104:{//设备副ID[5]
				send_data[index++] = (confInfo->fctData.BtNum[8])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[9])&0xff;
			}break;
			case 1105:{//设备副ID[6]
				send_data[index++] = (confInfo->fctData.BtNum[10])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[11])&0xff;
			}break;
			case 1106:{//设备副ID[7]
				send_data[index++] = (confInfo->fctData.BtNum[12])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[13])&0xff;
			}break;
			case 1107:{//设备副ID[8]
				send_data[index++] = (confInfo->fctData.BtNum[14])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[15])&0xff;
			}break;
			case 1108:{//设备副ID[9]
				send_data[index++] = (confInfo->fctData.BtNum[16])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[17])&0xff;
			}break;
			case 1109:{//设备副ID[10]
				send_data[index++] = (confInfo->fctData.BtNum[18])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[19])&0xff;
			}break;
			case 1110:{//设备副ID[11]
				send_data[index++] = (confInfo->fctData.BtNum[20])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[21])&0xff;
			}break;
			case 1111:{//设备副ID[12]
				send_data[index++] = (confInfo->fctData.BtNum[22])&0xff;
				send_data[index++] = (confInfo->fctData.BtNum[23])&0xff;
			}break;
			case 1112:{//设备副ID[13]
				send_data[index++] = (0)&0xff;
				send_data[index++] = (0)&0xff;
			}break;
			default:{
				send_data[index++] = (0>>8)&0xff;
				send_data[index++] = 0&0xff;
			}break;
		}
	}
	unsigned short crcValue = XgBmsProtocolCrc(send_data, index);
	send_data[index++] = (crcValue >> 8)& 0xff;
	send_data[index++] = crcValue&0xff;
	
	modbus_send_data(send_data,index,comNum);
}

static void mTChargerData(unsigned char *data,unsigned short dataLen,uint8_t comNum){
	uint8_t funcCode = data[1];
	switch(funcCode){
		case READSW://读开关量
			sendSwitchValue(data,dataLen,comNum);		
		break;
		case READREG://读寄存器地址
			sendAnalogValue(data,dataLen,comNum);
		break;
	}

}




//明唐充电机解析
void mt_charger_data(uint8_t *data,uint16_t len,uint8_t comNum){
	SysRunData_t  *runInfo = getSysRunData();
	SysConfData_t *confinfo = getSysConfData();
	if(data[0] != 1){//地址不为1
		return;
	}
	if(data[1]>127 || data[1]<1){//功能码为1-127
		printf(" MingTang charger data invalid func code\n");
		return;
	}
	int mtCrcValue = XgBmsProtocolCrc(data,len - 2);
	char crcL = mtCrcValue & 0xff;
	char crcH = (mtCrcValue >> 8) & 0xff;
	if (crcH != data[len - 2] || crcL != data[len - 1]) {//crc校验不通过
		printf("MingTang charger data crc check not pass\n");
		return ;
	}
	runInfo->comLinkState.Bits.rs485_obc_timeout = 0;//超时状态置0
	runInfo->comLinkState.Bits.rs485_obc_link = 1;//连接状态置1
	mTChargerData(&data[0],len,comNum);
}


			